package com.lanou.controller;

import com.github.pagehelper.PageInfo;
import com.lanou.bean.AdminInfo;
import com.lanou.service.AdminInfoService;
import com.lanou.service.RoleService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by dllo on 18/3/9.
 */
@Controller
public class AdminController {
    @Resource
    private AdminInfoService adminInfoService;

    @RequestMapping(value = {"/", "/index"})
    public String index() {
        return "index";
    }

    @RequestMapping(value = "/login")
    public String login() {
        if (SecurityUtils.getSubject().isAuthenticated()) {
            return "redirect:index";
        }
        return "login";
    }

    @RequestMapping(value = "/loginsubmit")
    public String loginsubmit(HttpServletRequest request) throws Exception {
        String exName = (String) request.getAttribute("shiroLoginFailure");
        if (UnknownAccountException.class.getName().equals(exName)) {
            throw new IncorrectCredentialsException("用户名或密码错误，请重试");
        } else if (IncorrectCredentialsException.class.getName().equals(exName)) {
            throw new IncorrectCredentialsException("用户名或密码错误，请重试");
        } else {
            throw new Exception("网络错误");
        }
    }

    @ResponseBody
    @RequestMapping(value = "/json")
    public List<AdminInfo> admininfo(@RequestParam("username") String username) {
        return adminInfoService.findAdminById(username);
    }

    @ResponseBody
    @RequestMapping(value = "/adminPage")
    public PageInfo<AdminInfo> adminPage(@RequestParam("pagenum") Integer pageNo) {
        return adminInfoService.queryAdminByPage(pageNo, 3);
    }

    @RequestMapping(value = "/adminList")
    public String manage() {
        return "/admin/admin_list";
    }

    @ResponseBody
    @RequestMapping(value = "/adminMsg")
    public List<AdminInfo> adminMsg() {
        return adminInfoService.findAllAdmin();
    }

    @RequestMapping(value = "/adminAdd")
    public String add() {
        return "/admin/admin_add";
    }

    @ResponseBody
    @RequestMapping(value = "/addAdmin")
    public void addAdmin(@RequestParam("admin") String admin, @RequestParam("password") String password, @RequestParam("name") String name, @RequestParam("telephone") String telephone, @RequestParam("email") String email, @RequestParam("roles[]") List<Integer> roles) {
        System.out.println("admin" + admin + "password" + password + "name" + name + "tel" + telephone + "email" + email + "roles" + roles);
        adminInfoService.addAdmin(admin, password, name, telephone, email, roles);
    }

    @RequestMapping(value = "/adminUpdate")
    public String adminUpdate() {
        return "/admin/admin_modi";
    }

    @ResponseBody
    @RequestMapping(value = "/updateAdmin")
    public void updateAdmin(@RequestParam("admin") String admin, @RequestParam("name") String name, @RequestParam("telephone") String telephone, @RequestParam("email") String email, @RequestParam("adminId") Integer adminId, @RequestParam("roles[]") List<Integer> roles) {
        adminInfoService.updateAdmin(admin, name, telephone, email, adminId, roles);
    }

    @ResponseBody
    @RequestMapping(value = "/deleteAdmin")
    public void deleteAdmin(@RequestParam("adminId") Integer adminId) {
        adminInfoService.deleteAdmin(adminId);
    }

    @ResponseBody
    @RequestMapping(value = "/selectAdminRoleByName")
    public List<AdminInfo> selectAdminRoleByName(String username) {
        Subject subject = SecurityUtils.getSubject();
        AdminInfo adminInfo = (AdminInfo) subject.getPrincipal();
        username = adminInfo.getAdminCode();
        return adminInfoService.selectAdminRoleByName(username);
    }

    @RequestMapping(value = "/userInfo")
    private String userInfo() {
        return "/user/user_info";
    }

    @ResponseBody
    @RequestMapping(value = "/updateUserInfo")
    public void updateUserInfo(@RequestParam("name") String name, @RequestParam("telephone") String telephone, @RequestParam("email") String email, String adminCode) {
        Subject subject = SecurityUtils.getSubject();
        AdminInfo adminInfo = (AdminInfo) subject.getPrincipal();
        adminCode = adminInfo.getAdminCode();
        adminInfoService.updateUserInfo(name, telephone, email, adminCode);
    }

    @RequestMapping(value = "/userModiPwd")
    private String userModiPwd() {
        return "/user/user_modi_pwd";
    }

    @ResponseBody
    @RequestMapping(value = "/selectOldPasswordExist")
    public boolean selectOldPasswordExist(@RequestParam("password") String password, String adminCode) {
        Subject subject = SecurityUtils.getSubject();
        AdminInfo adminInfo = (AdminInfo) subject.getPrincipal();
        adminCode = adminInfo.getAdminCode();
        List<AdminInfo> list = adminInfoService.selectOldPasswordExist(password, adminCode);
        System.out.println(list);
        if (!list.isEmpty()) {
            return true;
        } else {
            return false;
        }
    }

    @ResponseBody
    @RequestMapping(value = "updatePassword")
    public void updatePassword(@RequestParam("password") String password, String adminCode) {
        Subject subject = SecurityUtils.getSubject();
        AdminInfo adminInfo = (AdminInfo) subject.getPrincipal();
        adminCode = adminInfo.getAdminCode();
        adminInfoService.updatePassword(password, adminCode);
    }

    @RequestMapping(value = "/resetpass")
    public String resetPwd(@RequestParam("admins[]") List<Integer> admins) {

        adminInfoService.resetpass(admins);
        return "admin/admin_list";
    }

}